/**
 * Copyright 2008-2009. Chongqing Communications Industry Services Co.,Ltd Information Technology Branch. All rights
 * reserved. <a>http://www.cqcis.com</a>
 */
package com.cqcis.uip.base.framework.utils;

import org.apache.mina.common.IoSession;

import com.cqcis.uip.base.framework.common.Task;

/**
 * 连接工具类
 * 
 * @author huangmy
 * @date   2008-5-10
 */
public class SessionUtil {
	private final static String BIND_TASK = SessionUtil.class.getName() + "_BIND_TASK";
	private final static String CURRENT_TASK = SessionUtil.class.getName() + "_CURRENT_TASK";	
	private final static String CLOSED_BY_SERVICE = SessionUtil.class.getName() + "_CLOSED_BY_SERVICE";
	private final static String STATE_WAITING_TASK = SessionUtil.class.getName() + "_STATE_WAITING_TASK";
	private final static String TIMEOUT_CHECK_CLOSED = SessionUtil.class.getName() + "_TIMEOUT_CHECK_CLOSED";
	
	/**
	 * session状态空闲
	 * 
	 * @param session
	 */
	public static void setSessionStateWaitingTask(IoSession session) {
		session.setAttribute(STATE_WAITING_TASK, "true");
	}
	
	/**
     * 设置非空闲
     * 
     * @param session
     * @return
     */
    public static void removeSessionStateWaitingTask(IoSession session) {
    	session.removeAttribute(STATE_WAITING_TASK); 
    }
    
    /**
     * 状态是否空闲
     * 
     * @param session
     * @return
     */
    public static boolean isSessionStateWaitingTask(IoSession session) {
    	return session.getAttribute(STATE_WAITING_TASK) != null ? true : false;
    }
	
	/**
	 * 应用主动关闭
	 * 
	 * @param session
	 */
	public static void setClosedByService(IoSession session) {
		session.setAttribute(CLOSED_BY_SERVICE, "true");
	}
	
	/**
	 * 是否应用主动关闭
	 * 
	 * @param session
	 * @return
	 */
	public static boolean isClosedByService(IoSession session) {
		return session.getAttribute(CLOSED_BY_SERVICE) != null ? true : false;
	}
	
	/**
	 * 相关联的task
	 * 
	 * @param session
	 * @return
	 */
    public static Task getBindTask(IoSession session) {
    	return (Task)session.getAttribute(BIND_TASK);
    }
    
    /**
     * 删除相关联的task
     * 
     * @param session
     * @return
     */
    public static Task removeBindTask(IoSession session) {
    	return (Task)session.removeAttribute(BIND_TASK);
    }
    
    /**
     * 设置相关联的task
     * 
     * @param session
     * @param task
     */
    public static void setBindTask(IoSession session, Task task) {
    	session.setAttribute(BIND_TASK, task);
    }
    
    /**
     * 设置session的当前task
     * 
     * @param session
     * @param task
     */
    public static void setCurrentTask(IoSession session, Task task) {
    	session.setAttribute(CURRENT_TASK, task);
    }
    
    /**
	 * 相关联的task
	 * 
	 * @param session
	 * @return
	 */
    public static Task getCurrentTask(IoSession session) {
    	return (Task)session.getAttribute(CURRENT_TASK);	
    }
    
    /**
     * 删除相关联的task
     * 
     * @param session
     * @return
     */
    public static Task removeCurrentTask(IoSession session) {
    	return (Task)session.removeAttribute(CURRENT_TASK);
    }
    
    /**
     * 关闭超时检查
     * 
     * @param session
     * @param check
     */
    public static void setTimeoutCheckClose(IoSession session, boolean check) {
    	session.setAttribute(TIMEOUT_CHECK_CLOSED, Boolean.valueOf(check));
    }
    
    /**
     * 是否已经关闭了超时检查
     * 
     * @param session
     * @return
     */
    public static boolean isTimeoutCheckClosed(IoSession session) {
    	Boolean check = (Boolean)session.getAttribute(TIMEOUT_CHECK_CLOSED);
    	if (check == null) {
    		return false;
    	}
    	
    	return check;
    }
}
